[#import "/framework/JGP.htm"  as JGP]
<!--
项目   parent
作者   LOUFEI [Gavin]
时间   2018/4/13
-->
[@JGP.head "Title"/]
[@JGP.body]
    <jgp-div _col="['50','50']" _row="['30','70','*']" _runit="px">
        <jgp-tools slot="1-1">
            <jgp-btn _fn="save">保存</jgp-btn>
        </jgp-tools>
        <div slot="2-1">
            <jgp-query _target="resGridGroup" _action="/security/admin/adminApiController/queryResGroupByPowerType">
                <jgp-form-group _title="资源组">
                    <jgp-drop _col="12" _name="powerType" _placeholder="资源类型" _readonly="true"
                              _list="[{'label':'--请选择资源类型--','value':''},
                               {'label':'访问控制','value':'PAGE'},
                               {'label':'异步操作','value':'AJAX'},
                               {'label':'服务','value':'SERVICE'}]"></jgp-drop>
                </jgp-form-group>
            </jgp-query>
        </div>
        <jgp-grid slot="3-1" _id="resGridGroup" _check="false" _onclick="chooseGroup"></jgp-grid>

        <div slot="2-2">
            <jgp-query _target="resGrid" _id="queryForm" _action="/security/admin/adminApiController/queryResForChoose">
                <jgp-form-group _title="资源搜索">
                    <jgp-text _col="12" _name="remark" _placeholder="资源描述"></jgp-text>
                </jgp-form-group>
            </jgp-query>
        </div>
        <jgp-grid slot="3-2" _id="resGrid" _check="true" _onload="initSelected"></jgp-grid>
    </jgp-div>
[/@JGP.body]

[@JGP.foot]
    <script>
        var selected;

        function setSelected() {
            JGP.grid('resGrid').setSelected(selected);
        }

        function initSelected() {
            if (!selected) {
                JGP.$post('/security/admin/adminApiController/queryRoleRes/${roleId!''}', {}, function (result) {
                    selected = result.data.resIds;
                    setSelected(selected);
                })
            } else {
                setSelected(selected);
            }
        }

        function chooseGroup(result) {
            JGP.query('queryForm').field('group', result.back.group);
        }

        function save() {
            var resGroup = JGP.grid('resGridGroup').getFocusRecords();
            if (resGroup.length == 0) {
                JGP.$alert('必须选择资源');
                return;
            }
            var selectedRecords = JGP.grid('resGrid').getSelected();
            var resIds = [];
            for (var i = 0; i < selectedRecords.length; i++) {
                resIds.push(selectedRecords[i].id);
            }
            JGP.$get('/security/admin/adminApiController/saveRoleRes', {
                        resIds: resIds + '',
                        resGroup: resGroup[0].group, roleId: '${roleId!''}'
                    },
                    function () {
                        JGP.$alert('权限修改成功');
                        location.reload();
                    }, function () {
                        JGP.$error('错误');
                    })
        }
    </script>

[/@JGP.foot]